package com.nbgs.demo.service.impl;

import com.nbgs.demo.dao.CountOverdueMapper;
import com.nbgs.demo.info.CountOverdueInfo;
import com.nbgs.demo.service.CountOverdueService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.temporal.ChronoUnit;
import java.util.List;

@Service
@Slf4j
public class CountOverdueServiceImpl implements CountOverdueService {

    @Autowired
    private CountOverdueMapper countOverdueMapper;

    @Override
    public void countOverdueInfo() {
        List<CountOverdueInfo> countOverdueInfos = countOverdueMapper.selectOverdueInfo();

        log.info("查找结果："+countOverdueInfos);

        for (CountOverdueInfo info :countOverdueInfos) {

            Timestamp billGenerateTime = info.getBillGenerateTime();

            String[] times = new SimpleDateFormat("yyyy-MM-dd").format(billGenerateTime).split("-");

            LocalDate generateTime = LocalDate.of(Integer.parseInt(times[0]),Integer.parseInt(times[1]),Integer.parseInt(times[2]));
            System.out.println("订单生成的时间：" + generateTime);

            String[] time = new SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()).split("-");

            LocalDate newTime = LocalDate.of(Integer.parseInt(time[0]),Integer.parseInt(time[1]),Integer.parseInt(time[2]));
            System.out.println("现在的时间：" + newTime);

            long daysDiff = ChronoUnit.DAYS.between(generateTime, newTime);

            log.info("时间间隔为："+daysDiff+"天");

            if (daysDiff>20){
                double overdue = info.getOverdue()+0.5;
                countOverdueMapper.updateInfo(overdue,info.getBillId());
            }
        }

    }
}
